package com.hq.myapplicationdemo.utils;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateFormatUtils {

    private static String defaultStrFormat = "yyyy-MM-dd HH:mm:ss";
    private static final String minStr = "分钟前";
    private static final String hourStr = "小时前";
    private static final String dayStr = "天前";

    public static String getFormatYear(String timeStr){
        if(TextUtils.isEmpty(timeStr)) return timeStr;
        try {
            long time = new SimpleDateFormat(defaultStrFormat).parse(timeStr).getTime();
            return new SimpleDateFormat("yyyy-MM-dd").format(new Date(time));
        } catch (ParseException e) {
            return timeStr;
        }
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getCurrentStr() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(defaultStrFormat);

        return simpleDateFormat.format(new Date());
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getCurrentStr(String strFormat) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(strFormat);

        return simpleDateFormat.format(new Date());
    }

    //获取当天（按当前传入的时区）00:00:00所对应时刻的long型值
    public static long getStartTimeOfDay(long now) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(now);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /****
     * 获取系统的日期
     * @return
     */
    public static String getDate() {
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String year = String.valueOf(cal.get(Calendar.YEAR));
        String month = String.valueOf(cal.get(Calendar.MONTH)) + 1;
        String day = String.valueOf(cal.get(Calendar.DATE));
        String data = year + month + day;
        return data;
    }

    public static long getTime(String timeStr) {
        return getTime(timeStr, defaultStrFormat);
    }

    public static long getTime(String timeStr, String strFormat) {
        if (TextUtils.isEmpty(timeStr)) return 0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(strFormat);
        try {
            return simpleDateFormat.parse(timeStr).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 获取时间差
     *
     * @return n小时前 或 n天前
     */
    public static String getDiffStringTime(long time) {
        if (time == 0) return null;
        long diffTime = System.currentTimeMillis() - time;
        long minTime = diffTime / 1000 / 60;
//        if (minTime == 0) return "刚刚";
//        if (minTime < 60) return minTime + minStr;
        if (minTime <2*60) return "刚刚";
        if (minTime < 24 * 60) {
            return minTime / 60 + hourStr;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        return simpleDateFormat.format(time);
    }
}
